/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.internal.renderer;

import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.web.gwt.form.server.description.Description;

/**
 * The combo box cell renderer.
 */
@SuppressWarnings("serial")
public class ComboBoxRenderer
    extends JLabel
    implements ListCellRenderer
{

  /**
   * reference to gui to get i18n components name.
   */
  private FormEditorGUI gui;



  /**
   * Handle the display of the data.
   * 
   * @param list
   *          The list of data.
   * @param value
   *          The selected value.
   * @param index
   *          The selected value index.
   * @param isSelected
   *          Whether the data is selected.
   * @param cellHasFocus
   *          Whether the data has the focus.
   * @return the component.
   */
  public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
  {
    this.setOpaque(true);

    if (isSelected) {
      this.setBackground(list.getSelectionBackground());
      this.setForeground(list.getSelectionForeground());
    }
    else {
      this.setBackground(list.getBackground());
      this.setForeground(list.getForeground());
    }

    if (value instanceof Description) {
      Description description = (Description) value;
      this.setText(getGui().getController().notifyTranslation(description.getName()));
    }
    else {
      this.setText("");
    }
    return this;
  }



  /**
   * Constructor.
   * 
   * @param gui
   *          to get i18n components name.
   */
  public ComboBoxRenderer(FormEditorGUI gui)
  {
    setGui(gui);
  }



  /**
   * getter for the gui parameter.
   * 
   * @return gui property
   */
  public FormEditorGUI getGui()
  {
    return gui;
  }



  /**
   * setter for the gui parameter.
   * 
   * @param gui
   *          property
   */
  public void setGui(FormEditorGUI gui)
  {
    this.gui = gui;
  }

}
